<!DOCTYPE html>
<html>
<head>
    <title>添加收费项目--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
    $(function () {       
		$('#backid').click(function(){
				window.location.href="charge.html";
		 });
    });
    </script>
</head>
<body>
<form  class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">编号</td>
        <td><input type="text" id="chapId" value="" readonly="readonly"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">收费项目名称</td>
        <td><input type="text" id="chapName" value="" /></td>
    </tr>

    <tr>
        <td width="10%" class="tableleft">收费金额</td>
        <td><input type="text" id="chapMoney" value=""/></td>
    </tr>
    <tr>
        <td colspan="2">
			<center>
				<button  class="btn btn-primary" id="sub" type="button" >保存</button> &nbsp;
                &nbsp;<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
			</center>
		</td>
    </tr>
</table>
</form>
</body>
</html>
<script>

    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }

    $(function() {
        let url = document.location.toString();
        let chapId = url.GetValue("chapId");
        queryById(chapId);
        // 监听提交按钮
        subListen();
    });
    function queryById(chapId){
        $.ajax({
            type: "GET",
            url: origin + "/charge/"+chapId, //RESTful风格的API定义
            data: {jwtToken:jwtToken},
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if(data.code == 200){
                    loadDate(data);
                }else{
                }
            }
            // TODO: 后端拦截ajax请求进行登陆跳转需要前端相互配合, 每一个ajax请求都需要加上以下的代码
            ,complete : function(xhr, status) {
                myComplete(xhr, status);
            }
        });

    }
    let oldChapName;
    function loadDate(data) {
        let chap = data.obj;
        console.log(chap);
        let chapId=chap.chapId;
        let chapMoney=chap.chapMoney;
        let chapName=chap.chapName;
        $("#chapId").val(chapId);
        $("#chapName").val(chapName);
        $("#chapMoney").val(chapMoney);
        oldChapName = chapName;

    }

    // 修改提交
    function updateSub(chapName){
        let chapId = $("#chapId") .val();
        // let chapName = $("#chapName").val();
        let chapMoney = $("#chapMoney").val();
        layer.load(0);
        $.ajax({
            type: "POST",
            url: origin + "/charge/"+chapId, //RESTful风格的API定义
            data: {
                _method:"PUT",
                jwtToken:jwtToken,
                chapName:chapName,
                chapMoney:chapMoney,
            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: "json",
            success: function (data) {
                layer.closeAll()
                console.log(data);
                if (data.code == 200){
                    window.location.href = "charge.html";
                }else{
                    layer.msg(data.msg, function(){})
                    if(data.code == 400){
                        $("#chapName").focus();
                    }
                }
            }
            ,complete : function(xhr, status) {
                myComplete(xhr, status);
            }
        });
    }

    // 监听提交按钮
    function subListen(){
        $("#sub").click(function(){
            let chapName = $("#chapName").val().trim();
            if(oldChapName.indexOf(chapName) != -1){
                updateSub(chapName);
            }else{
                $.ajax({
                    type: "GET",
                    url: origin + "/charge/chapname",
                    data: {
                        _method:"PUT",
                        chapName:chapName,
                        jwtToken:jwtToken
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll()
                        if(data.code == 400){
                            updateSub(chapName);
                        }else if(data.code == 200){
                            layer.msg("项目名称重复", function(){});
                        }
                    }
                    ,complete : function(xhr, status) {
                        myComplete(xhr, status);
                    }
                });
            }
        });
    }


</script>